import { IMiddleware, Middleware } from '@midwayjs/core';
import { Context, NextFunction } from '@midwayjs/koa';

@Middleware()
export class FormatMiddleware implements IMiddleware<Context, NextFunction> {


  resolve() {
    return async (ctx: Context, next: NextFunction) => {
      const result = await next();

      if (result.success || result.message) {
        return result;
      } else {
        return { success: true, message: 'OK', data: result };
      }
    };
  }


  static getName(): string {
    return 'format';
  }
}
